<template>
  <el-container>
    <el-main style="background-color: #47474b">
      <div class="list">
        <div class="list-item" v-for="(item,index) in games">
          <div class="box-item" @mouseover="mouseOver(index)" @mouseleave="mouseLeave(index)">
            <div class="box-card">
              <div style="width: 270px; height: 140px;">
                <img
                  @click="startGame(item.path) "
                  :src="item.main_image"
                  style="object-fit: cover;width: 100%;height: 100%;"
                />
                <div class="game-edit" v-show="isEdit">
                  <el-button
                    size="mini"
                    type="danger"
                    icon="el-icon-delete"
                    circle
                    @click="editGame(index)"
                  ></el-button>
                </div>
                <div class="game-name" v-show="currentIndex==index&&!isEdit">{{item.name}}</div>
              </div>
            </div>
          </div>
        </div>
        <!-- 添加btn -->
        <div class="list-item" @click="addGame">
          <div class="box-item">
            <div class="box-card">
              <div class="add-box">
                <span class="add-icon iconfont icon-add"></span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </el-main>
    <el-footer style="background-color: #47474b">
      <el-row>
        <el-col :span="24" style>
          <div class="edit-text" @click="isEdit=!isEdit">{{isEdit?"退出":"编辑"}}</div>
        </el-col>
      </el-row>
    </el-footer>
  </el-container>
</template>
<script>
const { execFile, exec } = require("child_process");
import axios from "axios";
import highLevel from "@/modules/high_level";
import fs from "@/modules/fs";
import set from "@/modules/set";
//330*170
export default {
  data() {
    return {
      currentIndex: -1,
      isEdit: false,
      games: []
    };
  },
  methods: {
    startGame(path) {
      if (fs.existsSync(path)) {
        highLevel.openUrl(path);
        return;
      }
      this.$message.error("您还没有安装这款游戏！");
    },
    addGame() {
      let that = this;
      set.selectGameFile(async function(path) {
        let r = await that.$prompt(
          "提示：软件会根据您输入的名称在游戏库中查找对应的游戏资料，请尽可能的输入正确。",
          "请输入游戏名称",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消"
          }
        );
        let res = await that.$http.post("/game/search", { game_name: r.value });
        console.log(res);
        if (res.data.total > 0) {
          that.games.push({
            name: r.value,
            main_image: res.data.games[0].cover,
            path: path
          });
          set.saveGames(that.games);
          return;
        }
      });
    },
    editGame(index) {
      this.games.splice(index, 1);
      set.saveGames(this.games);
    },
    mouseOver(index) {
      this.currentIndex = index;
    },
    // 移出
    mouseLeave(index) {
      this.currentIndex = -1;
    }
  },
  created() {
    this.games = set.getGames();
  }
};
</script>
<style  scoped>
.game-edit {
  height: 20px;
  line-height: 20px;
  position: relative;
  right: -235px;
  top: -138px;
}
.game-name {
  position: relative;
  font-size: 20px;
  left: 10px;
  bottom: 30px;
}
.edit-text {
  font-size: 18px;
  height: 60px;
  line-height: 60px;
  width: 50px;
  cursor: pointer;
  text-align: center;
  transition: all 0.3s;
}
.edit-text:hover {
  color: #fff;
}

.edit-text:active {
  color: #34cb31;
}

.add-icon {
  display: block;
  font-size: 60px;
  margin: 0 auto;
  -webkit-transition: font-size 0.25s linear, width 0.25s linear;
  -moz-transition: font-size 0.25s linear, width 0.25s linear;
  transition: font-size 0.25s linear, width 0.25s linear;
}
.add-box {
  width: 270px;
  height: 140px;
  display: flex;
  align-items: center;
  color: #868686;
}
.list {
  position: relative;
  width: 100%;
  line-height: 1;
  overflow: hidden;
}

.list-item {
  position: relative;
  width: 25%;
  vertical-align: top;
  display: inline-block;
  cursor: pointer;
}
.box-item {
  color: #fff;
  background-color: #fff;
  margin: 10px;
  transition: all 0.3s;
}
.box-card {
  width: 100%;
}
.box-item:hover {
  color: #fff;
  transform: scale(1.05);
}
.box-item:active {
  color: #34cb31;
}
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
::-webkit-scrollbar-track {
  border-radius: 5px;
  background-color: #46464a;
}
::-webkit-scrollbar-thumb {
  border-radius: 5px;
  background-color: #46464a;
}

::-webkit-scrollbar-thumb:hover {
  background-color: rgb(141, 140, 140);
}

::-webkit-scrollbar-thumb:active {
  background-color: rgb(145, 144, 144);
  cursor: pointer;
}
</style>